var urlDetail = server_host + '/userweb/wxfood/getWXOrderFoodInfo.do';
//退款
var urlRefund = server_host + '/userweb/foodview/unsubscribeorderfood.do';
//退订
var urlUnsubscribe = server_host + '/userweb/foodview/cancelorderfood.do';
//获取微信预支付信息
var urlWxPrePayInfo = domain_url + '/h5wx/getWXpayInfo.do';
//是否来自微信支付回调
var fromWxPay = getParameterByName('fromWxPay');
//订单号
var orderNum = getParameterByName('orderNum');
// 状态
var stateCode = getParameterByName('stateCode');
//剩余时间为0重加载数据
var urlReLoad = server_host + '/user/cancelTableOrder.do';

//微信预支付
var urlWxPrePay = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx497e8037bea463e7&redirect_uri=http://www.cheerhi.com/h5wx/callwxpay_food.do' + encodeURIComponent('?url=orderMealDetail.html') + '&response_type=code&scope=snsapi_base&state=' + orderNum + '#wechat_redirect';


var appModule = angular.module('CheerHiWx', []);
appModule.controller('OrderMealCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {


	//餐点状态
	$scope.mealStatusArr = [{
		code: 1,
		dscp: '订单未付款，未消费'
	}, {
		code: 2,
		dscp: '订单未付款，已消费'
	}, {
		code: 3,
		dscp: '订单已付款，未消费'
	}, {
		code: 4,
		dscp: '订单已付款，已消费'
	}, {
		code: 5,
		dscp: '退款中'
	}, {
		code: 6,
		dscp: '已退订'
	}, {
		code: 7,
		dscp: '已退款'
	}, {
		code: 8,
		dscp: '现场未付款,未消费'
	}, {
		code: 9,
		dscp: '现场未付款,已消费'
	}, {
		code: 10,
		dscp: '已现场付款,已消费'
	}];


	//餐点送达状态
	$scope.mealArriveStatus = [{
		code: 0,
		dscp: '未送达'
	}, {
		code: 1,
		dscp: '已送达'
	}];

	$scope.toDate = function(datetimeStr){
		var date = new Date();
		var dateStr = datetimeStr.split(' ')[0];
		var timeStr = datetimeStr.split(' ')[1];
		date.setFullYear(dateStr.split('-')[0]);
		date.setMonth(dateStr.split('-')[1] - 1);
		date.setDate(dateStr.split('-')[2]);
		date.setHours(timeStr.split(':')[0]);
		date.setMinutes(timeStr.split(':')[1]);
		date.setSeconds(timeStr.split(':')[2]);
		return date;
	}

	$scope.showTime = false;
	$scope.orderNum = orderNum;

	$scope.loadList = function() {
		$http.get(urlDetail + '?orderNum=' + orderNum).success(function(data, status, headers, config) {
			console.log(data);
			$scope.orderDetail = data.data;
			$scope.orderStatus = findElement($scope.mealStatusArr, 'code', data.data.orderState);

			if(($scope.toDate($scope.orderDetail.lastTime2).getTime() - (new Date()).getTime() < 15 * 1000 * 60) && ($scope.toDate($scope.orderDetail.lastTime2).getTime() - (new Date()).getTime() >= 0)){
				$scope.showTime = true;
				// 倒计时
				// var datetimeStr = '2015-09-07 11:00:45';
				var datetimeStr = $scope.orderDetail.lastTime2;
				//创建时间
				// var date = $scope.toDate(datetimeStr);
				//取消时间（十五分钟以后）
				$scope.expiryTime = $scope.toDate(datetimeStr);

				$scope.setLimiteTime = setInterval(function(){

					var currTime = new Date();
					var times = $scope.expiryTime.getTime() - currTime.getTime();
					var minutes = parseInt(times / (1000 * 60));
					var seconds = parseInt(times % (1000 * 60));
					// console.log(minutes + ':' + parseInt(seconds / 1000));

					if(times<=0){
						clearInterval($scope.setLimiteTime);
						$scope.limitTime = "00:00";
						// 剩余时间为0重加载数据
						$http.get(urlReLoad + '?orderNum=' + $scope.orderDetail.orderNum).success(function(data, status, headers, config){
							$scope.loadList();
						})
					}else{
						$scope.limitTime = minutes + ':' + parseInt(seconds / 1000);
					}
					$scope.$apply();

				},1000);
			}
			
		});
	}

	// 退订
	$scope.unsubscribe = function(orderNum) {
		var r=confirm("确定退单?");
		if (r==true){
			$http.post(urlUnsubscribe + '?orderNum=' + orderNum).success(function(data, status, headers, config) {
				console.log(data);
				if (data.data.isSuccess) {
					alert(data.data.msg);
					$scope.loadList();
					// location.href = 'my-order.html';
				}
			});
		}
	}

	// 退款
	$scope.refund = function(orderNum) {
		$http.get(urlRefund + '?orderNum=' + orderNum).success(function(data, status, headers, config) {
			console.log(data);
			
			if (data.data.isSuccess) {
				alert('退款申请成功！');
				$scope.loadList();
			} else {
				alert(data.data.msg);
			}
		});
	}
	// $scope.refund = function(orderNum) {
	// 	$http.get(urlRefund + '?orderNum=' + orderNum).success(function(data, status, headers, config) {
	// 		console.log(data);

	// 		if (data.data.isSuccess) {
	// 			alert('退款申请成功！');
	// 			$scope.loadList();
	// 		} else {
	// 			alert(data.data.msg);
	// 		}
	// 	});
	// }

	$scope.goPay = function() {
		location.href = 'pay-way.html?orderNum=' + orderNum;
	}

	$scope.findElement = findElement;

	// 点击『地图』
	$scope.openmap = function() {
		// location.href = 'map.html?shopId=' + shopId + '&shopName=' + $scope.shopDetails.shopName + '&lat=' + $scope.shopDetails.lat + '&lng=' + $scope.shopDetails.lng; 
		wx.openLocation({
			latitude: $scope.orderDetail.lat,
			longitude: $scope.orderDetail.lng,
			name: $scope.orderDetail.shopName,
			address: $scope.orderDetail.addr,
			scale: 20,
			infoUrl: ''
		});
	}

	$scope.loadList();

}]);

function wxJssdkInit() {
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: server_host + '/h5wx/getWxTicket.do?url=' + encodeURIComponent(location.href.split('#')[0]),
		success: function(data) {
			wx.config({
				debug: false,
				appId: wxAppId,
				timestamp: data.timestamp,
				nonceStr: data.nonceStr,
				signature: data.signature,
				jsApiList: wxJsApi
			});
		}
	});
}

if (typeof WeixinJSBridge == "undefined") {
	if (document.addEventListener) {
		document.addEventListener('WeixinJSBridgeReady', wxJssdkInit, false);
	} else if (document.attachEvent) {
		document.attachEvent('WeixinJSBridgeReady', wxJssdkInit);
		document.attachEvent('onWeixinJSBridgeReady', wxJssdkInit);
	}
} else {
	wxJssdkInit();
}